package com.example.competitionregistration.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * @Author QuanJingFang
 * @Description //TODO 生成令牌和解析令牌
 * @Date 2023/7/15 20:48
 **/
public class JwtUtil {
    private static String signKey="fhdakfdjhfjjagjasgkdjhasfdsggadfgaeargadgadgvd";
    
    /**
     * @Author QuanJingFang
     * @Description //TODO 生成令牌
     * @Date 2023/7/15 20:50 
     * @Param [claims]
     * @return java.lang.String
     **/
    public static String generate(Map<String,Object> claims){
        String Jwt= Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, signKey)
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + 1800000))
                .compact();
        return Jwt;
    }

    /**
     * @Author QuanJingFang
     * @Description //TODO 解析令牌
     * @Date 2023/7/15 20:59
     * @Param [Jwt]
     * @return io.jsonwebtoken.Claims
     **/
    public static Claims parse(String Jwt){
        Claims claim = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(Jwt)
                .getBody();
        return claim;
    }
}
